#include "const.h"
#include <iostream>
#include <thread>
#include "boost/asio.hpp"
#include "json/json.h"
#include <signal.h>



using boost::asio::ip::tcp;
bool b_stop_=false;
void SingalHandler(int signum)
{

    b_stop_=true;

}


//构建两个线程，一个用于读取数据，一个用于发送数据


int main()
{
    signal(SIGINT,SingalHandler);
    signal(SIGTERM,SingalHandler);
    try
    {

        boost::asio::io_context io_context;
        boost::asio::ip::tcp::socket socket(io_context);
        socket.connect(tcp::endpoint(boost::asio::ip::address::from_string("192.168.248.171"), 8888));
        //同步发送数据
        std::thread write_thread([&socket]()
        {
            while(!b_stop_)
            {
                Json::Value val;
                short msg_id=MSG_HELLO_WORLD;
                msg_id = boost::asio::detail::socket_ops::host_to_network_short(msg_id);

                val["id"]=MSG_HELLO_WORLD;
                val["data"]="hsl 666";
                std::string ret_str = val.toStyledString();
                const char * sendData=ret_str.c_str();
                char sendBuf[MAX_LENGTH]={0};
                short len = boost::asio::detail::socket_ops::host_to_network_short(strlen(sendData));
                //前两个字节为ID
                memcpy(sendBuf,&msg_id,HEAD_ID_LEN);
                //2-4字节为消息体长度
                memcpy(sendBuf+HEAD_ID_LEN,&len,HEAD_DATA_LEN);
                //后续的字节为消息体
                memcpy(sendBuf+HEAD_TOTAL_LEN,sendData,strlen(sendData));


                boost::asio::write(socket,boost::asio::buffer(sendBuf,HEAD_TOTAL_LEN+strlen(sendData)));
                std::this_thread::sleep_for(std::chrono::milliseconds(1000));
                
            }
        });
        //同步接收数据
        std::thread read_thread([&socket]()
        {
            while(!b_stop_)
            {
                char receiveHead[HEAD_TOTAL_LEN]={0};
                boost::asio::read(socket,boost::asio::buffer(receiveHead,HEAD_TOTAL_LEN));
                //0-2字节为消息ID
                short msgid=0;
                memcpy(&msgid,receiveHead,HEAD_ID_LEN);
                msgid=boost::asio::detail::socket_ops::network_to_host_short(msgid);
                //2-4字节为消息体长度
                short msgLen=0;
                memcpy(&msgLen,receiveHead+HEAD_ID_LEN,HEAD_DATA_LEN);
                msgLen=boost::asio::detail::socket_ops::network_to_host_short(msgLen);
                char msg[MAX_LENGTH]={0};
                boost::asio::read(socket,boost::asio::buffer(msg,msgLen));
                std::cout<<"recv msg len = "<<msgLen<<std::endl;
                std::cout<<"------------解析消息---------------\n";
                Json::Reader reader;
                Json::Value val;
                if(reader.parse(msg,val))
                {
                    std::cout<<">>id = "<<val["id"].asInt()<<std::endl;
                    std::cout<<">>data = "<<val["data"].asString()<<std::endl;
                }
                std::this_thread::sleep_for(std::chrono::milliseconds(950));
            }
        });


        //等待两个线程结束
        write_thread.join();
        read_thread.join();

        io_context.run();
        
    }
    catch (const std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }

}
